package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author shine
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        //查询护理计划关联的护理项目列表
        List<NursingProjectPlan> nursingProjectPlanList = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //数据封装
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanList.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
//        nursingPlan.setCreateTime(DateUtils.getNowDate());
        //新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(nursingPlan);
        //新增中间表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
//        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        //更新护理计划主表
        boolean flag = this.updateById(nursingPlan);

        //处理护理项目关联表(中间表) 先删旧关联,再添新关联
        if (dto.getProjectPlans() != null && !dto.getProjectPlans().isEmpty()) {
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().
                    eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            //通过护理计划id定位旧关联

            //给新关联的每个项目绑定id
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return flag ? 1 : 0;
    }

/*    *//**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     *//*
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }*/

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery()
        .in(NursingProjectPlan::getPlanId,id));
        return removeById(id) ? 1 : 0;
    }

}
